<?php

namespace app\api\controller;

use app\facade\Redis;
use app\facade\Mns;
use think\Validate;
use think\facade\Env;
use oss\Oss;
use app\models\{Banner, Annou, Kefu, Config, Price, Hall, Quotes, Order};

use think\Db;


class Other extends Base
{
    private $url = 'https://api-aws.huobi.pro/';
    public function Quotes()
    {
        $data = [];
        $type = $this->request->get('type/d', 0);
        $id = $this->request->get('id/d', 0);
        $rdslist = Redis::get('quotekline');
        $result = [];
        if ($rdslist) {
            $data = unserialize($rdslist);
            if ($type == 2) {
                foreach ($data as $k =>  $datas) {
                    if ($datas['ishot'] != 2) {
                        unset($data[$k]);
                    }
                }
            }
            if (!empty($id)) {
                foreach ($data as $datas) {
                    if ($datas['id'] == $id) {
                        $result = $datas;
                    }
                }
            }
        }
        if (empty($id)) {
            $data = array_merge($data);
        } else {
            $data = $result;
        }
        $this->result($data, 0, '获取成功');
    }

    public function QuotesIndex()
    {
        $id = $this->request->get('id/d', 0);
        $type = $this->request->get('type/d', 1);
        $size = 200; //数据长度
        if ($id == 0 || $type == 0) {
            $this->result([], 1, '参数错误');
        }
        $lists = (new Quotes())->where('id', $id)->find();
        if (empty($lists)) {
            $this->result([], 0, '获取成功');
        }
        if ($lists['type'] == 2) {
            $result = [];
            $rdscoinmart = 'cointokens' . $lists['id'] . 'type_' . $type;
            $valuerds = Redis::get($rdscoinmart); //获取值
            $result = $valuerds;
            if (empty($result)) {
                $data = [];
            } else {
                $data = unserialize($result);
                foreach ($data as $k => $datas) {
                    if ($datas['open'] == 0) {
                        unset($data[$k]);
                    }
                }
            }
        } else {
            $data = [];
            if ($type == 1) {
                $period = '1min';
            } elseif ($type == 5) {
                $period = '5min';
            } elseif ($type == 15) {
                $period = '15min';
            } elseif ($type == 30) {
                $period = '30min';
            } elseif ($type == 60) {
                $period = '60min';
            } elseif ($type == 1440) {
                $period = '1day';
            } else {
                $period = '';
            }
            $url = $this->url . "market/history/kline?symbol="
                . strtolower($lists['coin'] . $lists['mart']) . '&size=' . $size . '&period=' . $period;
            $result = api_curl($url);
            $client = json_decode($result, true);
            if (!empty($client['status']) && $client['status'] == 'ok') {
                $data = $client['data'];
            }
            foreach ($data as &$datas) {
                $datas['id'] = date('Y-m-d H:i:s', $datas['id']);
                unset($datas['amount']);
                unset($datas['count']);
            }
        }
        $data = array_merge($data);
        $last_names = array_column($data, 'id');
        array_multisort($last_names, SORT_ASC, $data);
        $this->result($data, 0, '获取成功');
    }

    public function oss()
    {
        if (empty($_FILES['file'])) {
            $this->result([], 1, '参数错误');
        }
        $file = $_FILES['file'];
        $oss = new Oss();
        $oss->setUploadType('ram');
        $url = $oss->uploadPostFile($file, 'ming_user/' . date('Y-m-d'));
        $this->result($url, 0, '获取成功');
        // $result = [];
        // try {
        //     $result = (new Oss())->getCredentials();
        //     $result['regionId'] = Env::get('aliyun.regionId');
        //     $result['bucket'] = Env::get('aliyun.bucket');
        //     $result['endpoint'] = Env::get('aliyun.endpoint');
        // } catch (\Exception $e) {
        //     $this->result([], 1, $e->getMessage());
        // }
        // $this->result($result, 0, '获取成功');
    }
    public function sendMns()
    {
        $data = $this->request->post();
        $validate = new Validate([
            'mobile' => 'require',
        ], [
            'mobile.require' => '手机号码必须填写',
        ]);
        if (!$validate->check($data)) {
            $this->result([], 1, $validate->getError());
        }
        $CheckCode = (new Mns())->send($data['mobile']);
        if (!$CheckCode || $CheckCode['status'] == false) {
            $this->result([], 1, $CheckCode['msg']);
        }
        $this->result([], 0, $CheckCode['msg']);
    }
    public function getcode()
    {
        $code = rand(100000, 999999);
        Redis::setex($code, 120, $code);
        $this->result(['code' => $code], 0, '获取成功');
    }

    public function banner()
    {
        $result = (new Banner())->field('id,name,url')->select();
        $this->result($result, 0, '获取成功');
    }


    public function annou()
    {
        $type = $this->request->get('type/d', 0);
        $result = (new Annou())->field('id,type,title,infos,times')->where('type', $type)->paginate(20, false, ['query' => $this->request->param()]);
        $this->result($result, 0, '获取成功');
    }

    public function annoudetail()
    {
        $id = $this->request->get('id/d', 0);
        $result = (new Annou())->field('id,type,title,infos,times')->where('id', $id)->find();
        $this->result($result, 0, '获取成功');
    }

    public function kefu()
    {
        $result = (new Kefu())->field('id,type,number')->select();
        $this->result($result, 0, '获取成功');
    }

    public function Price()
    {
        $endtime = date('Y-m-d');
        $startime = date('Y-m-d', time() - 86400);
        $result['list'] = (new Price())->field('date,price')->order('date desc')->whereBetweenTime('date', $startime, $endtime)
            ->limit(7)->select();
        $result['price'] = (new Price())->where('date', date('Y-m-d'))->value('price') ?? 0;
        $result['usdt'] = (new Config())->where('key_value', 'usdt')->value('value') ?? 6.45;
        $hallconfig = (new Config())->where('key_value', 'hallconfig')->value('value');
        $result['hallconfig'] = json_decode($hallconfig, true) ?? [];
        $result['paycacel'] = (new Config())->where('key_value', 'paycacel')->value('value') ?? 30;
        $result['finishtrue'] = (new Config())->where('key_value', 'finishtrue')->value('value') ?? 120;
        $result['starttime'] = (new Config())->where('key_value', 'starttime')->value('value') ?? '00:00';
        $result['endtime'] = (new Config())->where('key_value', 'endtime')->value('value') ?? '24:00';
        $this->result($result, 0, '获取成功');
    }

    public function version()
    {
        $result['version'] = (new Config())->where('key_value', 'version')->value('value') ?? '1.0.0';
        $result['android'] = (new Config())->where('key_value', 'android')->value('value') ?? '';
        $result['ios'] = (new Config())->where('key_value', 'ios')->value('value') ?? '';
        $this->result($result, 0, '获取成功');
    }
}
